<?php

namespace App\Admin\Controllers;

use Encore\Admin\Grid;
use App\Models\GoldRound;
use App\Models\GoldProduct;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\GoldProductAction\BatchMove;
use App\Admin\Actions\GoldProductAction\BatchTerminate;

class GoldProductsController extends AdminController
{
    protected $title = '金店商品记录';

    protected function grid()
    {
        $grid = new Grid(new GoldProduct());
        $grid->model()->orderByDesc('id');

        $grid->column('id', 'ID');
        $grid->column('gold_seed_id', '种子')->display(function ($value) {
	        return "{$value}:{$this->goldSeed->title}";
        });
        $grid->column('gold_round_id', '场次')->display(function ($value) {
	        return "{$value}:{$this->goldRound->title}";
        });
        $grid->column('started_at', '上架时间')->display(function ($value) {
	        return $value ? $this->started_at->toDatetimeString() : '';
        });
        $grid->column('disabled_at', '完结时间')->display(function ($value) {
	        return $value ? $this->disabled_at->toDatetimeString() : '';
        });
        $grid->column('created_at', '创建')->display(function () {
        	return $this->created_at->toDatetimeString();
        });

	    $grid->disableExport();
        $grid->disableCreateButton();
	    $this->gridFilter($grid);
        $this->gridActions($grid);
        $grid->disableActions();

	    $grid->paginate(50);

        return $grid;
    }

	protected function gridFilter(Grid $grid)
	{
		$grid->filter(function (Grid\Filter $filter) {
			$filter->expand();

            $filter->scope('active', '活动中')->whereNull('disabled_at');
            $filter->scope('disabled', '已终结')->whereNotNull('disabled_at');

			$filter->column(1/2, function (Grid\Filter $filter) {
				$filter->disableIdFilter();
				$filter->equal('gold_round_id', '场次')->select(GoldRound::all()->pluck('title', 'id'));
			});

			$filter->column(1/2, function (Grid\Filter $filter) {

			});
		});
	}

	protected function gridActions(Grid $grid)
	{
		$grid->batchActions(function (Grid\Tools\BatchActions $batch) {
			$batch->disableDelete();
			$batch->add(new BatchTerminate());
			$batch->add(new BatchMove());
		});
		$grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableAll();
		});
	}
}
